home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 October: Mac OS SDK / Dev.CD Oct 96 SDK / Dev.CD Oct 96 SDK1.toast / Development Kits (Disc 1) / MacTCP / MacTCP Developer Tools / HyperCard MacTCP Toolkit 1.0 / Source Code ƒ / alphaSort.c next >
Encoding:
C/C++ Source or Header  |  1994-11-21  |  4.5 KB  |  176 lines  |  [TEXT/MPS ]

  1. /*
  2.     alphaSort(direction,string) -- Sort the lines in string in either ascending (direction = "ascending") or
  3.         descending (direction = "descending") order.
  4.  
  5.     Written By Greg Kimberly
  6.     ©Apple Computer, Inc. 1988
  7.     All Rights Reserved.
  8.  
  9.     To compile and link this file using Macintosh Programmer's Workshop,
  10.     
  11.         c -b alphaSort.c
  12.     link -m ENTRYPOINT -o "TCP XCMD Example" -rt XFCN=7872 -sg alphaSort=Main,%A5Init,STDIO,INTENV ∂
  13.             alphaSort.c.o "{Libraries}HyperXLib.o" "{Libraries}Interface.o" "{CLibraries}CInterface.o" ∂
  14.             "{CLibraries}StdCLib.o"
  15.  
  16.     Initial coding 1988 by Greg Kimberly.
  17.     Modified for use in MacTCP Toolkit 3/27/89 by Harry Chesley.
  18. */
  19.  
  20. #include <Memory.h>
  21. #include <Packages.h>
  22. #include <Resources.h>
  23. #include <OSUtils.h>
  24. #include <HyperXCmd.h>
  25. #include <stdio.h>
  26. #include <string.h>
  27. #include <strings.h>
  28.  
  29. pascal void EntryPoint(XCmdPtr paramPtr)
  30.  
  31. {
  32.     Ptr oldA5, myA5;
  33.     Ptr A5Alloc();
  34.     void A5Init(Ptr myA5);
  35.     Ptr A5Swap (Ptr myA5);
  36.     void A5Dispose (Ptr myA5);
  37.  
  38.     /* Allocate and install our global variable space. */
  39.     myA5 = A5Alloc();
  40.     A5Init(myA5);
  41.     oldA5 = A5Swap(myA5);
  42.  
  43.     /* Do the sort. */
  44.     alphaSort(paramPtr);
  45.  
  46.     /* Deinstall and deallocate the globals. */
  47.     A5Swap(oldA5);
  48.     A5Dispose(myA5);
  49. }
  50.  
  51. alphaSort(paramPtr)
  52.  
  53. XCmdPtr paramPtr;
  54.  
  55. {
  56.     Ptr *pointerArray;        /* Array of pointers to line starting points. */
  57.     Handle myListHand;        /* Handle to input list. */
  58.     char *myListScan;        /* Pointer for scanning the list. */
  59.     Handle returnListHand;    /* Handle to the output. */
  60.     char *returnListScan;    /* Pointer for scanning through result. */
  61.     char adjective;                /* Pointer to type of sort requested string. */
  62.     long countCRs;                /* Count of number of lines. */
  63.     long i;
  64.     long (*compareFunc) ();
  65.     long compareAlphaUp();
  66.     long compareAlphaDown();
  67.     void errAbort();
  68.  
  69.     /* Check that we've got the right number of parameters. */
  70.     if (paramPtr->paramCount < 1) {
  71.         errAbort(paramPtr,"§§§ parameter count must be at least 1 §§§");
  72.         return;
  73.     };
  74.  
  75.     /* Get the parameters. */
  76.     myListHand = paramPtr->params[0];
  77.     if (paramPtr->paramCount == 1) adjective = 'a';
  78.     else if (paramPtr->params[1] == NULL) adjective = 'a';
  79.     else adjective = **(paramPtr->params[1]);
  80.  
  81.     /* Decide what type of sort to do. */
  82.     if ((adjective == 'd') || (adjective == 'D')) compareFunc = compareAlphaDown;
  83.         else if ((adjective == 'a') || (adjective == 'A')) compareFunc = compareAlphaUp;
  84.             else {
  85.                 errAbort(paramPtr,"§§§ unknown type of sort requested §§§");
  86.                 return;
  87.             };
  88.  
  89.     /* Allocate the result. */
  90.     returnListHand = NewHandle((long) 1 + strlen(*myListHand));
  91.     if (returnListHand == NULL) {
  92.         errAbort(paramPtr,"§§§ couldn't allocate result §§§");
  93.         return;
  94.     };
  95.  
  96.     /* Count the number of lines. */
  97.     countCRs = 0;
  98.     myListScan = *myListHand;
  99.     while (*myListScan)
  100.         if (*myListScan++ == '\n') countCRs++;
  101.  
  102.     /* Allocate an array of pointers into the lines. */
  103.     pointerArray = (Ptr *) NewPtr((countCRs + 1) * sizeof(Ptr));
  104.     if (pointerArray == NULL) {
  105.         DisposHandle(returnListHand);
  106.         errAbort(paramPtr,"§§§ out of memory §§§");
  107.         return;
  108.     };
  109.  
  110.     /* Fill in the pointers to the lines of the source text. */
  111.     countCRs = 0;
  112.     pointerArray[countCRs] = *myListHand;
  113.     countCRs = 1;
  114.     myListScan = *myListHand;
  115.     while (*myListScan) 
  116.         if (*myListScan++ == '\n')  {
  117.             *(myListScan-1) = 0;
  118.             if (*myListScan != 0)    /* Avoid '\n' that terminates list. */
  119.                 pointerArray[countCRs++] = myListScan;
  120.         };
  121.  
  122.     /* Sort the line pointers. */
  123.     HLock(myListHand);
  124.     qsort(pointerArray,countCRs,sizeof(Ptr),compareFunc);
  125.     HUnlock(myListHand);
  126.  
  127.     /* Transfer the sorted lines into the result handle. */
  128.     returnListScan = *returnListHand;
  129.     /* Note that countCRs isn't zero-based -- have to make up for the extra count. */                
  130.     for(i=0;i<countCRs;i++) {
  131.         myListScan = pointerArray[i];
  132.         while (*myListScan != 0) *returnListScan++ = *myListScan++;
  133.         *returnListScan++ = '\n'; /* Insert the '\n'. */
  134.     };
  135.     /* Put a terminator on the string. */
  136.     *(returnListScan-1) = 0;
  137.  
  138.     DisposPtr((Ptr) pointerArray);
  139.     paramPtr->returnValue = returnListHand;
  140. }
  141.  
  142. /* Compare routines called by quick sort. */
  143.  
  144. int compareAlphaDown(el1,el2)
  145.  
  146. char **el1,**el2;
  147.  
  148. {
  149.     return(-strcmp(*el1,*el2));
  150. }
  151.  
  152. int compareAlphaUp(el1,el2)
  153.  
  154. char **el1,**el2;
  155.  
  156. {
  157.     return(strcmp(*el1,*el2));
  158. }
  159.  
  160. /* Error handling: return the string as a result. */
  161.  
  162. void errAbort(paramPtr,str)
  163.  
  164. XCmdPtr paramPtr;
  165. char *str;
  166.  
  167. {
  168.     Handle  nuHndl;
  169.  
  170.     /* Allocate space for an error message, copy the string into it, and return the handle to HyperCard. */
  171.     nuHndl = NewHandle((long)(strlen(str)+1));
  172.     if (nuHndl == nil) return;
  173.     strcpy((char *)*nuHndl,str);
  174.     paramPtr->returnValue = nuHndl;
  175. }
  176.